package index

import (
	"github.com/gin-gonic/gin/binding"
	"github.com/go-playground/validator/v10"
	"time"
)

type Param struct {
	Id   int       `form:"id" json:"id" binding:"required"`
	Name string    `form:"name" json:"name" binding:"required"`
	Time time.Time `form:"time" json:"time" binding:"checkTime" time_format:"2006-01-02 15:04:05"`
}

// 自定义验证器
var checkTime validator.Func = func(fl validator.FieldLevel) bool {
	date, ok := fl.Field().Interface().(time.Time)
	if ok {
		today := time.Now()
		if today.After(date) {
			return false
		}
	}
	return true
}

func init() {
	if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
		v.RegisterValidation("checkTime", checkTime)
	}
}
